<?php

namespace app\common\model\goods;

use app\common\model\User;
use app\common\model\user\Level;
use think\Model;


class Cart extends Model
{





    // 表名
    protected $name = 'cart';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text'
    ];



    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function goods()
    {
        return $this->belongsTo('app\common\model\server\Goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public static function addCart($param)
    {
        extract($param);
        if (!isset($goods_id) && empty($goods_id)) {
            exception('参数错误');
        }
        if (!isset($num) || $num < 0) {
            exception('数量有误');
        }
        $model = new self;
        $cart = $model->where('goods_id', $goods_id)->where('user_id', $uid)->where('type', $type)->find();
        if ($cart) {
            if ($param['num'] <= 0) {
                $cart->delete();
            } else {
                $cart->allowField(true)->save(['num' => $param['num']]);
            }
            return $cart->id;
        } else {
            $data = [
                'user_id' => $uid,
                'goods_id' => $goods_id,
                'num' => $num,
                'type' => $type,
            ];
            if ($num > 0) {
                $model->allowField(true)->save($data);
                return  $model->id;
            }
        }
    }

    //删除购物车
    public static function delCart($uid)
    {
        $model = new self;
        return $model->where('user_id', $uid)->delete();
    }
    //购物车列表
    public static function list($uid)
    {
        $model = new self;
        $list = $model->with(['goods'])->whereIn('user_id', $uid)->where('type', '1')->select();
        return $list;
    }

    //获取购物车下单商品信息
    public static function getGoods($data, $uid, $type)
    {
        if ($type == 1) {
            $carts = Cart::list($uid);
        } else {
            extract($data);
            $cart_id = Cart::addCart([
                'goods_id' => $goods_id,
                'num' => $num,
                'type' => $type,
                'uid' => $uid,
            ]);
            $carts = Cart::with('goods')->where('cart.id', $cart_id)->select();
        }
        $user = User::get($uid);
        $level = Level::get($user->level_id);
        $discount = $level ? bcdiv($level->discount, 100) : 0;
        foreach ($carts as &$v) {
            if ($v->goods->status == 'hidden') {
                exception('商品【' . $v->goods->title . '】已下架，不能下单');
            }
            if ($v->goods->deletetime != null) {
                exception('商品【' . $v->goods->title . '】已删除，不能下单');
            }
            $v->totalMoney = $v->totalVipDeduction = $v->totalVipMoney = $v->totalVipDeduction = 0;
            //会员优惠金额
            if ($level && $discount > 0) {
                $vip_deduction = bcmul(bcmul($v->num, $v->goods->pay_money, 2), $discount);
                $vip_money = bcsub(bcmul($v->num, $v->goods->pay_money, 2),  $vip_deduction);
                $v->totalMoney +=  bcmul($v->num, $v->goods->pay_money, 2);;
                $v->totalVipDeduction +=  $vip_deduction;
                $v->totalVipMoney +=  $vip_money;
            } else {
                $v->totalMoney = bcmul($v->num, $v->goods->pay_money, 2);
            }
        }
        return $carts;
    }
}
